package com.ioidea.room.bean

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Entity
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.PrimaryKey
import androidx.room.Query
import androidx.room.TypeConverters
import androidx.room.Update
import kotlinx.coroutines.flow.Flow
import java.io.Serializable

@Entity
@TypeConverters(StrategyScheduleConverters::class)
data class GroupStrategy(
    @PrimaryKey(autoGenerate = true)
    val strategyId: Int? = null,
    val plcSn: String,
    val groupId: Int,
    val groupName: String,
    val groupStrategyName: String,
    val repeatWeekDays: String,
    val scheduleList: List<StrategySchedule>,
    var isExecuting: Boolean = false //当前是否是执行中的策略
) : Serializable

@Dao
interface GroupStrategyDao {


    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertGroupStrategy(groupStrategy: GroupStrategy): Long

    @Update
    fun replaceGroupStrategy(groupStrategy: GroupStrategy)

    @Query("DELETE FROM GroupStrategy WHERE strategyId==:strategyId")
    fun deleteGroupStrategyByStrategyId(strategyId: Int)

    @Query("DELETE FROM GroupStrategy WHERE groupId==:groupId")
    fun deleteGroupStrategyByGroupId(groupId: Int)


    @Query("SELECT * FROM GroupStrategy WHERE groupId==:groupId And plcSn==:plcSn")
    fun getAllGroupStrategyListByGroupId(groupId: Int, plcSn: String): List<GroupStrategy>

    @Query("SELECT * FROM GroupStrategy WHERE groupId==:groupId And plcSn==:plcSn")
    fun getAllGroupStrategyListFlowByGroupId(groupId: Int,plcSn: String): Flow<MutableList<GroupStrategy>>

    @Query("SELECT * FROM GroupStrategy WHERE strategyId==:strategyId")
    fun getGroupStrategyByStrategyId(strategyId: Int): Flow<GroupStrategy>

}
